package com.dlna.client.player;

import java.util.ArrayList;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.dlna.client.R;
import com.dlna.client.activity.base.BaseActivity;
import com.dlna.client.enity.FileInfo;
import com.dlna.client.util.Constant;
import com.dlna.client.view.CustomVideoView;

public class PlayerActivity extends BaseActivity implements OnErrorListener,
        OnCompletionListener, OnPreparedListener
{
    
    private static final String TAG = "PlayerActivity";
    
    public static PlayerActivity instance;
    
    private CustomVideoView vv_video;
    
    private int currVideoPosition = 0;
    
    //    private MediaController mController;
    
    private ArrayList<FileInfo> localVideoList = new ArrayList<FileInfo>();
    
    private boolean hasPicture;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// ȥ��������
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_player);
        vv_video = (CustomVideoView) findViewById(R.id.vv_video);
        vv_video.setOnPreparedListener(this);
        vv_video.setOnErrorListener(this);
        vv_video.setOnCompletionListener(this);
        
        //     // ʵ��MediaController
        //        mController = new MediaController(this);
        //        
        //     // ΪVideoViewָ��MediaController
        //        vv_video.setMediaController(mController);
        //        // ΪMediaControllerָ�����Ƶ�VideoView
        //        mController.setMediaPlayer(vv_video);
        
        Intent intent = getIntent();
        localVideoList = intent.getParcelableArrayListExtra(Constant.VIDEOINFOS);
        hasPicture = intent.getBooleanExtra(Constant.HAS_PICTURE, false);
        startVideoView();
        instance = this;
    }
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        stopVideoView();
        localVideoList = intent.getParcelableArrayListExtra(Constant.VIDEOINFOS);
        hasPicture = intent.getBooleanExtra(Constant.HAS_PICTURE, false);
        currVideoPosition = 0;
        startVideoView();
        super.onNewIntent(intent);
    }
    
    private void startVideoView()
    {
        if (currVideoPosition >= localVideoList.size())
        {
            finishPlayActivity();
            return;
        }
        vv_video.setVideoSize(0, 0);
        FileInfo fileInfo = localVideoList.get(currVideoPosition);
        if (!TextUtils.isEmpty(fileInfo.filePath))
        {
            // ���ò�����ƵԴ��·��
            vv_video.setVideoPath(fileInfo.filePath);
        }
        else if (!TextUtils.isEmpty(fileInfo.httpUrl))
        {
            // ���ò�����ƵԴ��·��
            vv_video.setVideoPath(fileInfo.httpUrl);
        }
        
    }
    
    private void stopVideoView()
    {
        if (null != vv_video)
        {
            vv_video.seekTo(0);
            vv_video.stopPlayback();
        }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                finishPlayActivity();
                break;
            
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        stopVideoView();
    }
    
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra)
    {
        if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
        {
            
        }
        else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED)
        {
            
        }
        showLongToast(R.string.toast_video_error);
        return true;
    }
    
    @Override
    public void onCompletion(MediaPlayer mp)
    {
        stopVideoView();
        currVideoPosition++;
        startVideoView();
    }
    
    @Override
    public void onPrepared(MediaPlayer mp)
    {
        Log.e(TAG, "==========onPrepared");

		int videoHeight = mp.getVideoHeight();
		int videoWidth = mp.getVideoWidth();
		vv_video.setVideoSize(videoWidth, videoHeight);
		if (videoHeight > videoWidth) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		} else {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		vv_video.start();
    }
    
    private void finishPlayActivity()
    {
        if (hasPicture)
            setResult(RESULT_OK);
        finish();
    }
    
    public void onVideoStart(int pos)
    {
        if (null != vv_video)
        {
//            vv_video.seekTo(pos);
            vv_video.start();
        }
    }
    
    public void onVideoPause(int pos)
    {
        if (null != vv_video)
        {
//            vv_video.seekTo(pos);
            vv_video.pause();
        }
    }
    
    public void onVideoSeekTo(int pos)
    {
        if (null != vv_video)
        {
            vv_video.seekTo(pos);
        }
    }
}
